package com.linux.huhx.main;

import com.sun.jersey.spi.container.servlet.ServletContainer;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;

/**
 * Created by huhx on 2017-05-15.
 */
public class HttpServer {

    private int serverPort = 8090;

    private String webRoot = "huhx";

//    public void start() throws Exception {
//        System.out.println("**********begin init*****************");
//        long beginTime = System.currentTimeMillis();
//        Server server = new Server(serverPort);
//        WebAppContext webAppContext = new WebAppContext();
//        webAppContext.setSessionHandler(new SessionHandler());
//        webAppContext.setContextPath("/" + this.webRoot);
//        // 这里面为什么加上huhx-web
//        webAppContext.setResourceBase("huhx-web/src/main/webapp");
//        webAppContext.setDefaultsDescriptor("huhx-web/src/main/webapp/WEB-INF/webdefault.xml");
//        webAppContext.setDescriptor("huhx-web/src/main/webapp/WEB-INF/web.xml");
//        webAppContext.setConfigurationDiscovered(true);
//        webAppContext.setParentLoaderPriority(true);
//
//        server.setSessionIdManager(new HashSessionIdManager());
//        server.setHandler(webAppContext);
//        server.start();
//        long endTime = System.currentTimeMillis();
//        System.out.println("************ start costs " + (endTime - beginTime) + "milliseconds, webroot is " + this.webRoot + ", port at " + this.serverPort + " ************");
//    }

    public void start() throws Exception {
        Server server = new Server(serverPort);
        ServletHolder servlet = new ServletHolder(ServletContainer.class);
        servlet.setInitParameter("com.sun.jersey.config.property.resourceConfigClass", "com.sun.jersey.api.core.PackagesResourceConfig");
        servlet.setInitParameter("com.sun.jersey.config.property.packages", "com.linux.huhx.business.action");
        ServletContextHandler handler = new ServletContextHandler(ServletContextHandler.SESSIONS);
        handler.setContextPath("/huhx");
        handler.addServlet(servlet, "/*");
        server.setHandler(handler);
        server.start();
        System.out.println("start...in 82");
    }

    public static void main(String[] args) throws Exception {
        HttpServer httpServer = new HttpServer();
        httpServer.start();
    }
}
